package com.snok.emenu;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;

public class ImgRectangle {
	//private FloatBuffer vertexBuffer;   // buffer holding the vertices
	private ByteBuffer	vertexBuffer;   // buffer holding the vertices
	private ByteBuffer  texBuffer;
	private ByteBuffer	teximg;         //texture image
	private int			texwidth;		//texture width
	private int			texheight;		//texture height
	private int[]       texturedatahandle = new int[1];
	DishGrid			mParent;
	


	private float vertices[] = {

			-0.8f, -0.8f,  0.0f,        // V1 - bottom left

			-0.8f,  0.8f,  0.0f,        // V2 - top left

			0.8f, -0.8f,  0.0f,        // V3 - bottom right
			
			0.8f,  0.8f,  0.0f         // V4 - top right

	};
	
	private float text_vetex[] = {
			0.0f,  1.0f,
			0.0f,  0.0f,
			1.0f,  1.0f,
			1.0f,  0.0f
	};

	ImgRectangle(DishGrid parent,String imgname){
		// a float has 4 bytes so we allocate for each coordinate 4 bytes
		
		mParent = parent;

		vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4);

		vertexBuffer.order(ByteOrder.nativeOrder());

		// allocates the memory from the byte buffer
		FloatBuffer fb = vertexBuffer.asFloatBuffer();

		// fill the vertexBuffer with the vertices
		fb.put(vertices);
		
		// set the cursor position to the beginning of the buffer
		vertexBuffer.position(0);
		
		PrepareTexture(imgname);
	}
	
	ImgRectangle(DishGrid parent,ByteBuffer texture,int img_w,int img_h,float width,float height,float pos_x,float pos_y){
		// a float has 4 bytes so we allocate for each coordinate 4 bytes
		mParent = parent;
		
		PrepareTexture(texture);
		
	}
		
	ByteBuffer LoadTexture(String imgname){
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inScaled = false;
		Bitmap bmp = BitmapFactory.decodeFile(imgname,opt);
		
		if(null == bmp){
			return null;
		}
		
		texwidth = bmp.getWidth();
		texheight = bmp.getHeight();
		int bytesnum = bmp.getByteCount();
		ByteBuffer bytebuf = ByteBuffer.allocateDirect(bytesnum);
		bytebuf.order(ByteOrder.nativeOrder());
		bmp.copyPixelsToBuffer(bytebuf);
		bytebuf.position(0);
		return bytebuf;
	}	
	
	protected void PrepareTexture(String imgname){
		texBuffer = ByteBuffer.allocateDirect(text_vetex.length * 4);
		texBuffer.order(ByteOrder.nativeOrder());
		FloatBuffer fb = texBuffer.asFloatBuffer();
		fb.put(text_vetex);
		texBuffer.position(0);
		
		teximg = LoadTexture(imgname);
		if(null == teximg){
			return;
		}
		
		GLES20.glGenTextures(1,texturedatahandle,0);
	}
	
	protected void PrepareTexture(ByteBuffer texture){
		
		texBuffer = ByteBuffer.allocateDirect(text_vetex.length * 4);
		texBuffer.order(ByteOrder.nativeOrder());
		FloatBuffer fb = texBuffer.asFloatBuffer();
		fb.put(text_vetex);
		texBuffer.position(0);
		
		teximg = texture;
		if(null == teximg){
			return;
		}
		
		GLES20.glGenTextures(1,texturedatahandle,0);
	}
	
	protected void SetupTexture(){
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,texturedatahandle[0]);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_NEAREST);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
		GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGBA,texwidth,texheight, 0,GLES20.GL_RGBA,
				GLES20.GL_UNSIGNED_BYTE, teximg);
	}
	
	/** The draw method for the square with the GL context */
	public void draw(int program) {
		//GLES20.glEnableVertexAttribArray(0);
		
		SetupTexture();
		
		GLES20.glUseProgram(program);
		
	    // get handle to vertex shader's vPosition member
	    int mPositionHandle = GLES20.glGetAttribLocation(program, "vPosition");
	    int vTexturecoord = GLES20.glGetAttribLocation(program, "a_TexCoordinate");
	    int uTextureHandle = GLES20.glGetUniformLocation(program, "u_Texture");

	    // Prepare the triangle coordinate data
	    vertexBuffer.position(0);
	    GLES20.glVertexAttribPointer(mPositionHandle, 3,
	                                 GLES20.GL_FLOAT, false,
	                                 0, vertexBuffer);
	    
	    // Enable a handle to the triangle vertices
	    GLES20.glEnableVertexAttribArray(mPositionHandle);
	    
	    // Prepare the texture coord
	    texBuffer.position(0);
	    GLES20.glVertexAttribPointer(vTexturecoord, 2,
	                                 GLES20.GL_FLOAT, false,
	                                 0, texBuffer);
	    
	    GLES20.glEnableVertexAttribArray(vTexturecoord);
	    
	    
	    //set the texture to unit 0
	    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
	    
	    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texturedatahandle[0]);

	    GLES20.glUniform1i(uTextureHandle, 0);
	    
		// Point to our vertex buffer
		//GLES20.glVertexAttribPointer(0,vertexBuffer.limit(),GLES20.GL_FLOAT, false, 0,vertexBuffer);

		// Draw the vertices as triangle strip
		GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
	    //GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);

		//Disable the client state before leaving
		//GLES20.glDisableClientState(GLES20.GL_VERTEX_ARRAY);
		GLES20.glDisableVertexAttribArray(mPositionHandle);
		
		GLES20.glDisableVertexAttribArray(vTexturecoord);
	}
	
}

